package com.ssy.lingxi.report.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.api.vo.request.PopularShopRequest;
import com.ssy.lingxi.report.constant.Constants;
import com.ssy.lingxi.report.dao.merchantmember.MemberDAO;
import com.ssy.lingxi.report.dao.order.ProcurementOrderDAO;
import com.ssy.lingxi.report.dao.platformtemplate.MemberShopDAO;
import com.ssy.lingxi.report.entity.report.PopularShopDO;
import com.ssy.lingxi.report.model.bo.MemberBO;
import com.ssy.lingxi.report.model.bo.MemberOrderBO;
import com.ssy.lingxi.report.model.bo.MemberShopBO;
import com.ssy.lingxi.report.model.vo.request.MemberIdRoleIdVO;
import com.ssy.lingxi.report.model.vo.response.PopularShopVO;
import com.ssy.lingxi.report.repository.report.PopularShopRepository;
import com.ssy.lingxi.report.service.IPopularShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 人气店铺接口实现
 * @author 伍将
 * @date 2020/11/3
 * @version 2.0.0
 */
@Service
public class PopularShopServiceImpl implements IPopularShopService {

    @Resource
    private PopularShopRepository repository;

    @Resource
    private ProcurementOrderDAO procurementOrderDAO;

    @Resource
    private MemberDAO memberDAO;

    @Resource
    private MemberShopDAO memberShopDAO;

    private static final Logger logger = LoggerFactory.getLogger(PopularShopServiceImpl.class);

    /**
     * 获取人气店
     * @author 伍将
     * @date 2020/11/2
     * @return com.ssy.lingxi.common.response.Wrapper<java.util.List < com.ssy.lingxi.report.model.vo.response.PopularShopVO>>
     **/
    @Override
    public Wrapper<List<PopularShopVO>> getPopularShopList(Integer count) {

        if (null == count) {
            count = Constants.WEB_POPULAR_SHOP_COUNT;
        }

        Pageable page = PageRequest.of(0, count);

        List<PopularShopVO> resultList = repository.findAll(page).stream().map(r -> {
            PopularShopVO query = new PopularShopVO();
            query.setMemberId(r.getMemberId());
            query.setRoleId(r.getRoleId());
            //query.setMemberShopId(r.getMemberShopId());
            query.setMemberName(r.getMemberName());
            query.setMemberLogo(r.getMemberLogo());

            return query;
        }).collect(Collectors.toList());

        return Wrapper.success(resultList);
    }


    /**
     * 人气店铺任务
     * @author 伍将
     * @date 2020/11/4
     * @param startTime:
     * @param count:
     **/
    @Override
    public void popularShopTask(Long startTime, Integer count) {

        // step 1：查询人气店铺
        List<MemberOrderBO> boList = procurementOrderDAO.getOrderTopMember(startTime, count);
        if (boList == null || boList.size() <= 0) {
            logger.info("没有查询到人气店铺");
            return;
        }
        logger.info("查询到人气店铺:{}条", boList.size());

        List<Long> memberIds = new ArrayList<>();
        List<MemberIdRoleIdVO> shopParamList = new ArrayList<>();

        List<PopularShopDO> doList = boList.stream().map(r -> {

            memberIds.add(r.getMemberId());

            MemberIdRoleIdVO shopParam = new MemberIdRoleIdVO();
            shopParam.setMemberId(r.getMemberId());
            shopParam.setRoleId(r.getRoleId());
            shopParamList.add(shopParam);


            PopularShopDO entity = new PopularShopDO();
            entity.setMemberId(r.getMemberId());
            entity.setRoleId(r.getRoleId());
            return entity;
        }).collect(Collectors.toList());

        // step 2：查询会员名称
        List<MemberBO> memberBOList = memberDAO.getMemberList(memberIds);
        if (memberBOList == null || memberBOList.size() <= 0) {
            logger.info("没有查询到对应会员");
            return;
        }

        // step 3：查询店铺信息
        List<MemberShopBO> shopBOList = memberShopDAO.getMemberShopList(shopParamList);
        if (shopBOList == null || shopBOList.size() <= 0) {
            logger.info("没有查询到对应店铺");
            return;
        }

        // step 4：组装数据
        List<PopularShopDO> list = new ArrayList<>();
        for (PopularShopDO r : doList) {
            memberBOList.stream().filter(o -> o.getId().equals(r.getMemberId())).findFirst().ifPresent(member -> r.setMemberName(member.getName()));
            MemberShopBO shop = shopBOList.stream().filter(
                    o -> o.getMemberId().equals(r.getMemberId())
                            && o.getRoleId().equals(r.getRoleId()))
                    .findFirst().orElse(null);
            if (shop != null) {
                r.setMemberShopId(shop.getId());
                r.setMemberLogo(shop.getLogo());
            }
            if (r.getMemberLogo() != null) {
                list.add(r);
            }
        }
        doList = list;

        // step 5：数据入库
        if (doList.size() <= 0) {
            logger.info("没有需要更新的人气店铺");
        } else {
            logger.info("需要更新的人气店铺:{}条", doList.size());
            repository.deleteAll();
            repository.saveAll(doList);
        }
    }

    /**
     * 获取人气店铺id列表
     * @author 伍将
     * @date 2020/12/28
     * @param request: 请求参数
     **/
    @Override
    public Wrapper<List<Long>> getPopularShopIdList(PopularShopRequest request) {
        Pageable page = PageRequest.of(0, request.getCount());
        return Wrapper.success(repository.findAll(page).stream().map(PopularShopDO::getMemberShopId).collect(Collectors.toList()));
    }


    /**
     * 获取人气店铺信息列表
     * @param request  请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<List<PopularShopDO>> getPopularShopListMsg(PopularShopRequest request) {
        Pageable page = PageRequest.of(0, request.getCount());
        Page<PopularShopDO> popularShopDOS = repository.findAll(page);
        List<PopularShopDO> content = popularShopDOS.getContent();
        return Wrapper.success(CollectionUtil.isNotEmpty(content)?content:new ArrayList<>());
    }
}
